package com.dingdang.controller.car_detail_management;


import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.TbBrand;
import com.dingdang.mapper.TbBrandMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("brand")
@Api("品牌分类接口")
public class BrandController
{
    @Autowired
    private TbBrandMapper brandMapper;

    @GetMapping
    @ApiOperation("品牌分类搜索")
    @ApiImplicitParam(name = "void",value = "空",required = true,paramType = "query")
    public Result getCarGoods()
    {
        List<TbBrand> brandList = brandMapper.selectList(null);
        return Result.response(ResultEnum.SUCCESS).setData(brandList);
    }

    //管理员增加
    @PostMapping
    @ApiOperation("品牌分类增加")
    @ApiImplicitParam(name = "TbCarGoods",value = "品牌分类实体类",required = true,paramType = "body")
    public Result insertAdmin(@RequestBody TbBrand brand)
    {
        int i = brandMapper.insert(brand);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //管理员回显
    @PostMapping("/toUpdate")
    @ApiOperation("品牌分类回显")
    @ApiImplicitParam(name = "id",value = "品牌分类id",required = true,paramType = "query")
    public Result ToUpdateAdmin(@RequestParam("id") Long id)
    {
        TbBrand brand = brandMapper.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(brand);
    }

    //管理员修改
    @PutMapping
    @ApiOperation("品牌分类修改")
    @ApiImplicitParam(name = "TbBrand",value = "品牌分类实体类",required = true,paramType = "body")
    public Result UpdateAdmin(@RequestBody TbBrand brand)
    {
        int i = brandMapper.updateById(brand);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @DeleteMapping()
    @ApiOperation("品牌分类删除")
    @ApiImplicitParam(name = "id",value = "品牌分类id",required = true,paramType = "query")
    public Result deleteInfo(@RequestParam(value = "id") Long []ids)
    {
        List<Long> deleteBatch = new ArrayList<>();
        for (Long addr:ids)
        {
            deleteBatch.add(addr);
        }
        int i = brandMapper.deleteBatchIds(deleteBatch);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }
}
